package com.masterclient.app.businessmodule.usermodule.login.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.eoner.baselibrary.utils.ViewTransformUtil;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.masterclient.app.App;
import com.masterclient.app.R;
import com.masterclient.app.base.ConfirmDialog;
import com.masterclient.app.businessmodule.usermodule.login.beans.InviteInfoData;
import com.masterclient.app.businessmodule.usermodule.login.beans.InviterUserInfoPopModel;
import com.masterclient.app.businessmodule.usermodule.login.beans.LoginSuccessData;
import com.masterclient.app.widget.CircleTransform;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import jp.wasabeef.glide.transformations.CropCircleWithBorderTransformation;

/**
 * 作者：hxm
 * 创建时间：2020/7/16
 * 功能说明：
 */

public class InviteUserDialog extends Dialog {


    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.tv_name)
    TextView tvName;

    @BindView(R.id.gzg_layout)
    View gzgLayout;


    private OnChooseListener onChooseListener;
    private Context context;
    private boolean hasInputInviteCode = false;
    private ConfirmDialog confirmDialog;
    private String popType;
    private String inviteCode = "";
    private LoginSuccessData loginSuccessData;

    public void setOnChooseListener(OnChooseListener onChooseListerner) {
        this.onChooseListener = onChooseListerner;
    }

    public InviteUserDialog(@NonNull Context context, OnChooseListener onChooseListener) {
        super(context, R.style.MyDialog);
        this.onChooseListener = onChooseListener;
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_invite_user);
        ButterKnife.bind(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Window window = getWindow();
        WindowManager.LayoutParams windowParams = window.getAttributes();
        windowParams.width = ScreenUtils.getScreenWidth();
        window.setGravity(Gravity.CENTER);
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setAttributes(windowParams);
    }

    /**
     * 设置邀请人弹窗信息
     * @param data 登录成功数据
     */
    public void setData(LoginSuccessData data) {
        loginSuccessData = data;
        if (data == null || (data.getInviter_code_select_pop() == null && data.getInviter_user_info_pop() == null)) {
            Toast toast = Toast.makeText(context, "网络异常", Toast.LENGTH_LONG);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
            dismiss();
            return;
        }

        if (data.isIs_show_user_info_pop() && data.getInviter_user_info_pop() != null) {
            //非选择弹窗
            popType = data.getInviter_user_info_pop().userPopType;
            InviterUserInfoPopModel inviterUserInfoPopModel = data.getInviter_user_info_pop();
            if(inviterUserInfoPopModel != null){
                inviteCode = inviterUserInfoPopModel.inviterCode;
            }
            if ("4".equals(popType)) {
                //4、绑定邀请人弹窗
                if (!TextUtils.isEmpty(inviterUserInfoPopModel.inviterAvatar)) {
                    ViewTransformUtil.glideImageView(getContext(),inviterUserInfoPopModel.inviterAvatar, ivHead,
                            new CropCircleWithBorderTransformation(0, 0xFFFFFF), R.mipmap.icon_n_logo);
                }
                tvName.setText(inviterUserInfoPopModel.inviterNickname);
            }
        }
    }

    public void refreshData(InviteInfoData data) {

        ViewTransformUtil.glideImageView(getContext(),data.getImage(), ivHead,
                new CropCircleWithBorderTransformation(0, 0xFFFFFF), R.mipmap.icon_n_logo);
        tvName.setText(data.getNick_name());
    }

    /**
     * 刷新邀请人信息
     * @param inviteInfoData  邀请人信息
     * @param notice 文案
     */
   public void refreshInviteInfo(InviteInfoData inviteInfoData,String notice) {
        this.popType = "4";
        if (inviteInfoData == null) {
            Toast toast = Toast.makeText(context, "网络异常", Toast.LENGTH_LONG);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
            dismiss();
            return;
        }

        //4、绑定邀请人弹窗
        inviteCode = inviteInfoData.getInvite_code();

        if (!TextUtils.isEmpty(inviteInfoData.getImage())) {
            App.picasso.load(inviteInfoData.getImage()).transform(new CircleTransform()).error(R.mipmap.user_head_default_icon0).placeholder(
                    R.mipmap.user_head_default_icon0).into(ivHead);
        }

       if (!TextUtils.isEmpty(inviteInfoData.getImage())) {
           ViewTransformUtil.glideImageView(getContext(),inviteInfoData.getImage(), ivHead,
                   new CropCircleWithBorderTransformation(0, 0xFFFFFF), R.mipmap.icon_n_logo);
       }
        tvName.setText(inviteInfoData.getNick_name());

    }



    @OnClick({R.id.tv_cancel, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
                dismiss();
                onChooseListener.cancel();
                break;
            case R.id.tv_confirm:
                onChooseListener.confirm(inviteCode);
                break;
        }
    }

    public interface OnChooseListener {
        void cancel();

        void confirm(String inviteCode);
    }
}
